package com.renting.modules.sys.web;

import java.util.List;

import com.renting.common.announce.LoginRequired;
import com.renting.common.entity.ResultList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.renting.common.entity.Result;
import com.renting.modules.sys.entity.User;
import com.renting.modules.sys.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * users
     *
     * @return
     * @description: 首页查询所有
     * @exception:
     * @author administrator
     * @create date： 2018年3月13日 @remark：
     */

    @RequestMapping(value = "v1/private/users", method = RequestMethod.GET)
    @ResponseBody
    @CrossOrigin(origins = "*")
    public String users() {
        ResultList resultList = new ResultList();
        List<User> users = userService.findAll();
        resultList.setData(users);
        return JSONObject.toJSONString(resultList);
    }

    /**
     * users
     *
     * @param
     * @return
     * @description:查询
     * @exception:
     * @author administrator
     * @create date： 2018年3月8日 @remark：
     */
    @RequestMapping(value = "v1/private/users/id/{id}", method = RequestMethod.GET)
    @ResponseBody
    public String users(User user) {
        user = userService.find(user);
        if (user == null) {
            return Result.errorInfo("用户不存在", "");
        }
        return JSONObject.toJSONString(user);
    }

    /**
     * 删除 怎么判断用户是否删除成功？
     *
     * @return
     */
    @RequestMapping(value = "v1/private/users", method = RequestMethod.DELETE)
    @ResponseBody
    @CrossOrigin(origins = "*")
    public String delete(HttpServletRequest request) {
        Result result = new Result(200);
        String ids = request.getHeader("ids");
        userService.delete(ids);
        return JSONObject.toJSONString(result);

    }

}
